# -*- coding: utf-8 -*-
import scrapy
from selenium import webdriver
import json
from ..items import BilibiliItem
import time

class BiSpider(scrapy.Spider):
    name = 'bi'
    allowed_domains = ['bilibili.com']
    #url1 = 'https://api.bilibili.com/x/web-interface/web/channel/category/channel_arc/list?id={}'
    start_urls = ['https://api.bilibili.com/x/web-interface/web/channel/category/channel_arc/list?id={}'.format(str(i)) for i in range(1,23)]

    def parse(self, response):
        print(self.start_urls)
        html = json.loads(response.text)
        item = BilibiliItem()
        item['kind_num'] = html['data']['total']
        list = html['data']['archive_channels']
        for li in list:
            item['kind'] = li['name']
            id = li['id']
            url2 = 'https://api.bilibili.com/x/web-interface/web/channel/multiple/list?channel_id={}&sort_type=hot&page_size=30'
            next_url = url2.format(id)
            print(next_url)
            yield scrapy.Request(url=next_url, meta={'item': item}, callback=self.info_parse)


    def info_parse(self,response):
        html = json.loads(response.text)
        item = response.meta['item']
        lists = html['data']['list'][1:-1]
        for li in lists:
            item['author'] = li['author_name']
            item['author_id'] = li['author_id']
            item['name'] = li['name']
            item['view_count'] = li['view_count']
            item['like_count'] = li['like_count']
            yield item